import Addnews from '@/components/Addnews.vue'
import Admin from '@/components/Admin.vue'
import AdminnewList from '@/components/AdminnewList.vue'
import Civil from '@/components/Civil.vue'
import Finance from '@/components/Finance.vue'
import FirstPage from '@/components/FirstPage.vue'
import Foreign from '@/components/Foreign.vue'
import Index from '@/components/Index.vue'
import Page1 from '@/components/page1.vue'
import Select from '@/components/Select.vue'
import Technology from '@/components/Technology.vue'
import UpdateInfo from '@/components/UpdateInfo.vue'
import Updatenews from '@/components/Updatenews.vue'
import { useUserStore } from '@/stores/userStore'
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
    {
        path: '/admin',
        component: Admin,
        children: [
            {
                path: '/adminnewlist',
                component: AdminnewList,
            },
            {
                path: '/addnews',
                component: Addnews,
            },
            {
                path: '/updatenews',
                component: Updatenews,
            },
            {
                path: '/updateinfo',
                component: UpdateInfo,
            },
        ]
    },
    {
        path: '/page1',
        component: Page1,
        meta: {
            requiresAuth: true
        },
    },
    {
        path: '/',
        component: Index,
        redirect: '/firstpage',
        children: [
            {
                path: 'firstpage',
                component: FirstPage,
            },
            {
                path: 'civil',
                component: Civil,
                meta: {
                    requiresAuth: true
                },
            },
            {
                path: 'foreign',
                component: Foreign,
                meta: {
                    requiresAuth: true
                },
            },
            {
                path: 'technology',
                component: Technology,
                meta: {
                    requiresAuth: true
                },
            },
            {
                path: 'finance',
                component: Finance,
                meta: {
                    requiresAuth: true
                },
            },
            {
                path: 'select',
                component: Select,
                meta: {
                    requiresAuth: true
                },
            },
        ]
    }
]

//创建路由对象
const router = createRouter({
    history: createWebHistory(),
    //注册路由表
    routes: routes,
})
router.beforeEach((to, from) => {
    // let islogin = window.localStorage.getItem("isLogin")
    const useStore = useUserStore()
    let isLogined = useStore.getIsLogin
    if (to.meta.requiresAuth && !JSON.parse(isLogined)) {
        //没有权限进入路由 返回首页
        alert("请登入")
        return {
            path: "/"
        }
    }
    console.log(to)
    return true
})
// 导出 路由对象
export default router
